import 'package:flutter/material.dart';
import 'indexedstack/indexedstack0.dart';
import 'indexedstack/indexedstack1.dart';
import 'indexedstack/indexedstack2.dart';

class TabBarIndexedStackDemo extends StatefulWidget {
  TabBarIndexedStackDemo({Key key}) : super(key: key);

  _TabBarIndexedStackDemoState createState() => _TabBarIndexedStackDemoState();
}

class _TabBarIndexedStackDemoState extends State<TabBarIndexedStackDemo> {
  int currentindex;

  @override
  void initState() {
    currentindex = 0;
    super.initState();
  }

  _handlechangeindex(index) {
    print('index== $index');
    if (currentindex == index) return;
    setState(() {
      currentindex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('TabBarIndexedStackDemo'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Container(
              child: Row(
                children: <Widget>[
                  FlatButton(
                    child: Text(
                      'tab0',
                      style: TextStyle(
                        color: currentindex == 0 ? Colors.red : Colors.black,
                      ),
                    ),
                    onPressed: () {
                      _handlechangeindex(0);
                    },
                  ),
                  FlatButton(
                    child: Text(
                      'tab1',
                      style: TextStyle(
                        color: currentindex == 1 ? Colors.red : Colors.black,
                      ),
                    ),
                    onPressed: () {
                      _handlechangeindex(1);
                    },
                  ),
                  FlatButton(
                    child: Text(
                      'tab2',
                      style: TextStyle(
                        color: currentindex == 2 ? Colors.red : Colors.black,
                      ),
                    ),
                    onPressed: () {
                      _handlechangeindex(2);
                    },
                  )
                ],
              ),
            ),
            Container(
              height: 200.0,
              width: double.infinity,
              color: Colors.red,
              // IndexedStack0,IndexedStack1,IndexedStack2 都会渲染，只是根据index来显示不同的内容,
              // 不会重复init 初始化
              child: IndexedStack(
                index: currentindex,
                alignment: Alignment.center,
                children: <Widget>[
                  IndexedStack0(),
                  IndexedStack1(),
                  IndexedStack2(),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
